package ServiceImpl

import (
	"context"
	"fmt"
	"gitee.com/banyanhouse/distill-micro-grpc-hystrix/micro_prod_server/Services"
	"strconv"
	"time"
)

type ProdService struct {
}

func (p *ProdService) GetProdsList(ctx context.Context, request *Services.ProdsRequest,
	response *Services.ProdListResponse) error {
	fmt.Printf("receive request:%d\n", request.Size)
	time.Sleep(time.Second * 3)
	models := make([]*Services.ProdModel, 0)
	// 模拟数据取数场景
	var i int32
	for i = 0; i < request.Size; i++ {
		models = append(models, newProd(100+i, "prodName"+strconv.Itoa(100+int(i))))
	}
	response.Data = models
	return nil
}

func (p *ProdService) GetProdDetail(ctx context.Context, req *Services.ProdsRequest, resp *Services.ProdDetailResponse) error {
	time.Sleep(time.Second * 3)
	resp.Data = newProd(req.ProdId, "测试商品详情-" + strconv.Itoa(int(req.ProdId)))
	return nil
}

// 测试方法
func newProd(id int32, pname string) *Services.ProdModel {
	return &Services.ProdModel{
		ProdId:   id,
		ProdName: pname,
	}
}
